package com.jdz.noise.tools;


import com.jdz.noise.dao.FlyDao;
import com.jdz.noise.dao.NoiseDao;
import com.jdz.noise.entities.GPS;
import com.jdz.noise.entities.FlyRow;

import javax.annotation.Resource;

public class Interpolation {

    @Resource
    private FlyDao flyDao;


    @Resource
    private NoiseDao noiseDao;


    public static final double PI = 3.14159265358979324;
    // 赤道半径(单位m)
    private static final  double EARTH_RADIUS = 6378137;

    /**
     * 转化为弧度(rad)
     * */
    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }





    //计算两个gps之间的距离
    public static double dis_time(GPS groundGps, GPS airGps)//将飞行参数插值到噪声中
    {
        return distance(groundGps,airGps)/340;
    }


    //计算声音传播的时间
    public static double dis_time(GPS groundGps, GPS airGps,double a_speed)//参数中包含声速
    {
        return distance(groundGps,airGps)/a_speed;
    }




    //计算声音传播的时间
    public static double distance(GPS groundGps, GPS airGps)//将飞行参数插值到噪声中
    {   //gps1,飞机坐标
        //gps2,声道坐标
        //经度，经分，经秒，纬度，纬分，纬秒，高度（秒）
        double time;
        double d;
        double lon1= groundGps.getLon();
        double lat1= groundGps.getLat();
        double h1 = groundGps.getH();//此处的H单位是，miao

        double lon2= airGps.getLon();
        double lat2= airGps.getLat();
        double h2 = airGps.getH();//此处的H单位是，miao

        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lon1) - rad(lon2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2)+Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
        s = s * EARTH_RADIUS;
        //s = Math.round(s * 10000) / 10000;
        d = Math.sqrt(Math.pow(s,2)+Math.pow(h1-h2,2));

        return d;
    }

    //测试FlyRow

    public static FlyRow getOne()
    {
        double[] data = new double[14];
        for(int i = 0;i<=13;i++)
        {
            data[i] = i*15;
        }

        FlyRow flyRow = new FlyRow(data);
        flyRow.setGPS_LAT_du(29);
        flyRow.setGPS_LAT_fen(16);
        flyRow.setGPS_LAT_miao(54.7981);
        flyRow.setGPS_LONG_du(117);
        flyRow.setGPS_LONG_fen(8);
        flyRow.setGPS_LONG_miao(53.6982);
        flyRow.setGPS_H_miao(246.118);

        return flyRow;
    }







    //拿到时间差，就可以对飞行参数进行操作了
    //首先，从第一个开始查询，然后for gps数组，针对每一个数组都计算时间差，计算到时间差后，然后根据数据的差值
    //根据这个数据差，然后用当前时间减去时间原点的值加上时间差，再加上飞参数据的时间阈值（如1/3秒）
    // 每次飞参都把从加上时间差到该加完的1/3秒钟所有的噪声数据的尾部填满飞参
    // 再把该条数据插入到新的表中即可



    public static double getBegin(FlyRow flyRow,double ori,GPS gps_ground)//FlyRow，时间原点，地面坐标
    {
        GPS airGps = new GPS(flyRow);

        System.out.println(flyRow.getTime());
        System.out.println(ori);
        System.out.println();

        System.out.println();

        System.out.println();

        System.out.println(dis_time(gps_ground,airGps));


        double begin = flyRow.getTime()-ori+dis_time(gps_ground,airGps);
        System.out.println("begin = "+begin);


        return begin;
    }





}
